"""
主菜单类：
    1. 迷宫规模选择
    2. 迷宫模式选择（人工，贪心AI，动归最优解）
    3. 使用 键盘实现即可
"""

from config import *
import sys
import pygame
from typing import List, Optional, Dict

class Menu:
    def __init__(self):
        """
        主菜单：里面定义了菜单的规模，字体
        用于选择菜单的配置
        """
        # 页面基础配置
        self.SCREEN_WIDTH = SCREEN_WIDTH_MENU
        self.SCREEN_HEIGHT = SCREEN_HEIGHT_MENU
        pygame.init()
        self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))
        pygame.display.set_caption('迷宫游戏主菜单')
        self.font_large = pygame.font.SysFont('kaiti', CELL_SIZE)
        self.font_small = pygame.font.SysFont('kaiti', CELL_SIZE // 4 * 3)
        # 难度和模式配置(需要输出)
        self.state = "start" # 状态控制 start -> difficulty -> mode -> done
        self.difficulty = ["简单(7x7)", "困难(15x15)"]
        self.selected_difficulty = 0

        self.mode = ["人工测试", "贪心求解演示", "动归最优解"]
        self.selected_mode = 0

        self.result_difficulty = None
        self.result_mode = None
        self.return_flag = False # 程序是否可以返回

    def draw_text(self, text, pos, selected = False, large = False):
        """
        :param large: 文本大小
        :param text: 要输出的文本
        :param pos: 文本显示的位置
        :param selected: 是否被选择
        """
        color = COLOR_GOLD if selected else COLOR_WHITE
        font = self.font_large if large else self.font_small
        img = font.render(text, True, color)
        rect = img.get_rect(center=pos)
        self.screen.blit(img, rect)

    def update_display(self):
        pygame.display.flip()

    def get_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if self.state == "start":
                    if event.key == pygame.K_RETURN:
                        self.state = "difficulty"

                elif self.state == "difficulty":
                    if event.key == pygame.K_LEFT and self.selected_difficulty > 0:
                        self.selected_difficulty -= 1
                    elif event.key == pygame.K_RIGHT and self.selected_mode < len(self.difficulty) - 1:
                        self.selected_difficulty += 1
                    elif event.key == pygame.K_RETURN:
                        self.state = "mode"
                    elif event.key == pygame.K_BACKSPACE:
                        self.state = "start"

                elif self.state == "mode":
                    if event.key == pygame.K_LEFT and self.selected_mode > 0:
                        self.selected_mode -= 1
                    elif event.key == pygame.K_RIGHT and self.selected_mode < len(self.mode) - 1:
                        self.selected_mode += 1
                    elif event.key == pygame.K_RETURN:
                        self.state = "done"
                    elif event.key == pygame.K_BACKSPACE:
                        self.state = "difficulty"

                elif self.state == "done":
                    if event.key == pygame.K_RETURN:
                        # 返回结果
                        self.result_difficulty = self.difficulty[self.selected_difficulty]
                        self.result_mode = self.mode[self.selected_mode]
                        self.return_flag = True
                    elif event.key == pygame.K_BACKSPACE:
                        self.state = "mode"

    # 先是规模 然后模式
    def display_menu(self) -> list:
        clock = pygame.time.Clock()
        while True:
            # 前期先用大白底
            self.screen.fill(COLOR_BOSS)
            if self.state == "start":
                self.draw_text("迷宫游戏", (self.SCREEN_WIDTH // 2, CELL_SIZE * 2), large=True)
                self.draw_text("按 [回车] 开始", (self.SCREEN_WIDTH // 2, 200))
            elif self.state == "difficulty":
                difficulty = self.difficulty
                self.draw_text("难度", (self.SCREEN_WIDTH // 2, CELL_SIZE * 2), large=True)
                self.draw_text(difficulty[0], (self.SCREEN_WIDTH // 4, 200), selected=(0 == self.selected_difficulty))
                self.draw_text(difficulty[1], (self.SCREEN_WIDTH // 4 * 3, 200), selected=(1 == self.selected_difficulty))
            elif self.state == "mode":
                mode = self.mode
                self.draw_text("模式", (self.SCREEN_WIDTH // 2, CELL_SIZE * 2), large=True)
                self.draw_text(mode[0], (self.SCREEN_WIDTH // 8, self.SCREEN_WIDTH // 8 * 3), selected=(0 == self.selected_mode))
                self.draw_text(mode[1], (self.SCREEN_WIDTH // 2, self.SCREEN_WIDTH // 8 * 3), selected=(1 == self.selected_mode))
                self.draw_text(mode[2], (self.SCREEN_WIDTH // 8 * 7 - 20, self.SCREEN_WIDTH // 8 * 3), selected=(2 == self.selected_mode))
            elif self.state == "done":
                self.draw_text("准备完成!", (self.SCREEN_WIDTH // 2, 80), large=True)
                self.draw_text(f"难度: {self.difficulty[self.selected_difficulty]}", (self.SCREEN_WIDTH // 2, 160))
                self.draw_text(f"模式: {self.mode[self.selected_mode]}", (self.SCREEN_WIDTH // 2, 200))
                self.draw_text("按 [回车] 开始游戏", (self.SCREEN_WIDTH // 2, 300))

            self.get_events()
            if self.return_flag:
                # pygame.quit() # 退出第一个界面
                return [self.selected_difficulty, self.selected_mode]
            self.update_display()

            clock.tick(60)

if __name__ == '__main__':
    menu = Menu()
    result = menu.display_menu()
    print(result)

